// 创建一个新文件实现这些类
export interface CellReference {
    sheet?: string;
    row: number;
    col: number;
    address?: string;
  }
  
  export class FormulaError extends Error {
    constructor(public error: string, message?: string) {
      super(message);
      this.name = 'FormulaError';
    }
  }
  
  export class DepParser {
    constructor(_options: any = {}) {}
  
    parse(formula: string, position: CellReference): any[] {
      const refs: any[] = [];
      // 使用正则表达式匹配单元格引用
      const cellPattern = /([A-Za-z]+[0-9]+)(?::([A-Za-z]+[0-9]+))?/g;
      let match;
  
      while ((match = cellPattern.exec(formula)) !== null) {
        if (match[2]) {
          // 范围引用 (例如 A1:B2)
          refs.push({
            from: this.parseCellAddress(match[1]),
            to: this.parseCellAddress(match[2]),
            sheet: position.sheet
          });
        } else {
          // 单个单元格引用
          refs.push({
            ...this.parseCellAddress(match[1]),
            sheet: position.sheet
          });
        }
      }
  
      return refs;
    }
  
    private parseCellAddress(address: string): {row: number; col: number; address: string} {
      const colMatch = address.match(/[A-Za-z]+/);
      const rowMatch = address.match(/[0-9]+/);
      
      if (!colMatch || !rowMatch) {
        throw new FormulaError('#REF!', '无效的单元格引用格式');
      }

      return {
        row: parseInt(rowMatch[0]),
        col: this.columnNameToNumber(colMatch[0]),
        address: address
      };
    }
  
    private columnNameToNumber(name: string): number {
      let sum = 0;
      for (let i = 0; i < name.length; i++) {
        sum *= 26;
        sum += name.charCodeAt(i) - 'A'.charCodeAt(0) + 1;
      }
      return sum;
    }
  }
  
  export default class FormulaParser {
    constructor(options: any = {}) {
      this.options = options;
    }
  
    private options: any;
  
    parse(expression: string, position: CellReference, evaluate = false): any {
      try {
        if (evaluate) {
          // 简单的表达式计算
          const context = {
            ...this.options.functions,
            ...this.createCellContext(position)
          };
          return this.evaluateExpression(expression, context);
        }
        return expression;
      } catch (error: unknown) {
        if (error instanceof Error) {
          throw new FormulaError('#ERROR!', error.message);
        }
        throw new FormulaError('#ERROR!', 'Unknown error occurred');
      }
    }
  
    private createCellContext(_position: CellReference) {
      return {
        getCell: (ref: CellReference) => {
          if (this.options.onCell) {
            return this.options.onCell(ref);
          }
          return null;
        },
        getRange: (ref: any) => {
          if (this.options.onRange) {
            return this.options.onRange(ref);
          }
          return [];
        }
      };
    }
  
    private evaluateExpression(expression: string, context: any): any {
      try {
        const fn = new Function('context', `with(context) { return ${expression}; }`);
        return fn(context);
      } catch (error) {
        throw new FormulaError('#ERROR!', '表达式计算错误');
      }
    }
  }